package cn.rxls.community.result;

import cn.rxls.community.enums.ResultEnum;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 统一返回数据类
 */
@NoArgsConstructor
@Data
public class Result<T> {
    //状态码
    private Integer code;
    //描述信息
    private String msg;
    //返回值
    private T data;

    Result(ResultEnum resultEnum) {
        this.code = resultEnum.code;
        this.msg = resultEnum.msg;
    }

    Result(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    Result(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    Result(ResultEnum resultEnum, T data) {
        this.code = resultEnum.code;
        this.msg = resultEnum.msg;
        this.data = data;
    }

    public static <T> Result<T> success(T data) {
        return new Result<>(ResultEnum.SUCCESS, data);
    }

    public static <T> Result<T> success() {
        return new Result<>(ResultEnum.SUCCESS);
    }

    public static <T> Result<T> success(ResultEnum resultEnum, T data) {
        return new Result<>(resultEnum, data);
    }

    public static <T> Result<T> success(Integer code, String msg) {
        return new Result<>(code, msg);
    }

    public static <T> Result<T> success(Integer code, String msg, T data) {
        return new Result<>(code, msg, data);
    }

    public static <T> Result<T> error() {
        return new Result<>(ResultEnum.ERROR, null);
    }

    public static <T> Result<T> error(ResultEnum resultEnum) {
        return new Result<>(resultEnum);
    }

    public static <T> Result<T> error(Integer code, String msg) {
        return new Result<>(code, msg);
    }
    public static <T> Result<T> error(ResultEnum resultEnum, List<String> msg) {
        return new Result<>(resultEnum.code, String.join(",", msg));
    }


}
